package com.shxy.controller;

import com.shxy.common.entity.CustomResult;
import com.shxy.common.enums.ResultCode;
import com.shxy.controller.ui.Pageable;
import com.shxy.controller.ui.StuInfo;
import com.shxy.entity.base.College;
import com.shxy.entity.base.Major;
import com.shxy.entity.custom.CustomClazz;
import com.shxy.entity.custom.CustomStudent;
import com.shxy.service.IQueryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 接口
 */
@RequestMapping("/v1")
@RestController
public class ApiController {
    @Autowired
    private IQueryService queryService;

    /**
     * 查询全部学院列表
     * @return
     */
    @GetMapping("/colleges/all")
    public CustomResult<List> getCollegeAll() {
        List<College> colleges = queryService.getCollegesAll();
        return new CustomResult(colleges);
    }

    /**
     * 查询全部专业信息
     * @return
     */
    @GetMapping("/majors/all")
    public CustomResult<List> getMajorsAll() {
        List<Major> majors = queryService.getMajorsAll();
        return new CustomResult(majors);
    }

    /**
     * 查询指定学院指定学年开设的专业列表
     * @param collegeId
     * @param year
     * @return
     */
    @GetMapping("/majors/college/{collegeId}/year/{year}")
    public CustomResult<List> getMajorsByCollegeAndYear(@PathVariable(name = "collegeId") int collegeId,
                                                             @PathVariable(name = "year") String year) {
        List<Major> majors = queryService.getMajorsByCollegeAndYear(collegeId, year);
        return new CustomResult(majors);
    }

    /**
     * 查询指定学年和专业的班级列表
     * @param majorId
     * @param year
     * @return
     */
    @GetMapping("/clazzs/major/{majorId}/year/{year}")
    public CustomResult<List> getClazzByMajorAndYear(@PathVariable(name = "majorId") int majorId,
                                                                @PathVariable(name = "year") String year) {
        List<CustomClazz> clazzes = queryService.getClazzsByMajorAndYear(majorId, year);
        return new CustomResult(clazzes);
    }

    /**
     * 查询指定班级的学生列表
     * @return
     */
    @GetMapping("/stus/clazz/{clazzId}")
    public CustomResult getStusByClazz(@PathVariable(name = "clazzId") int clazzId) {
        List<StuInfo> students = convert2StuInfos(queryService.getStusByClazz(clazzId));
        Pageable<StuInfo> pg = new Pageable<>(students.size(), students, 1, students.size());
        return new CustomResult(pg);
    }

    /**
     * 指定条件查询学生列表
     * @return
     */
    @GetMapping("/stus/query")
    public CustomResult<Pageable> queryStuList(@RequestParam(defaultValue = "") String year,
                                         @RequestParam(defaultValue = "0") int collegeId,
                                         @RequestParam(defaultValue = "0") int majorId,
                                         @RequestParam(defaultValue = "0") int clazzId,
                                         @RequestParam(defaultValue = "1") int page,
                                         @RequestParam(defaultValue = "10") int size) {
        // 1、查询分页的结果
        Pageable pageable = queryService.queryStusList(year, collegeId, majorId, clazzId, page, size);
        // 2、结果格式转换
        pageable.setItems(convert2StuInfos(pageable.getItems()));

        return new CustomResult(pageable);
    }

    /**
     * 根据学号查询学生信息
     * @return
     */
    @GetMapping("/stu/code/{code}")
    public CustomResult<StuInfo> queryStuInfo(@PathVariable(name = "code") String code) {
        StuInfo stuInfo = convert2StuInfo(queryService.getStuByCode(code).get());
        if(stuInfo == null){
            return new CustomResult(ResultCode.PARAM_NOT_VALID);
        }
        return new CustomResult(stuInfo);
    }

    /**
     * 格式转换
     * @param students
     * @return
     */
    List<StuInfo> convert2StuInfos(List<CustomStudent> students){
        return students.stream().map(cs -> {
            return convert2StuInfo(cs);
        }).collect(Collectors.toList());
    }

    /**
     * 学生信息格式转换
     * @param student
     * @return
     */
    StuInfo convert2StuInfo(CustomStudent student) {
        StuInfo si = new StuInfo();
        BeanUtils.copyProperties(student, si);
        si.setCollege(student.getClazz().getCollege().getName());
        si.setMajor(student.getClazz().getMajor().getName());
        si.setClazz(student.getClazz().getName());
        return si;
    }
}
